//
//  ByUIShopsDetailVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIShopsDetailVC.h"

// vc
#import "ByUIShopsPaySuccessVC.h"       // 付款成功
#import "ByUIPayVC.h"                   // 付款

// view
#import "ByViewShopsDetailHeader.h"
#import "ByViewShopsDeatilActivity.h"

// model
#import "ByModelShopsDetail.h"

// api
#import "ByApiShops.h"

@interface ByUIShopsDetailVC () <BMKLocationServiceDelegate>

@property (nonatomic, strong) UIScrollView *scroll;
@property (nonatomic, strong) ByViewShopsDetailHeader *header;
@property (nonatomic, strong) ByViewShopsDeatilActivity *activityView;


@property (nonatomic, strong) ByModelShopsDetail *detailModel;


// 实时定位
@property (nonatomic, strong) BMKLocationService *locService;

@end

@implementation ByUIShopsDetailVC

- (void)dealloc {
    
    [self removeNotify];
}

- (void)addNotify {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(headerDidLayout) name:KDetailHeaderDidLayout object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(activityDidLayout) name:KDetailActivityDidLayout object:nil];
}
- (void)removeNotify {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)headerDidLayout {
    
    self.activityView.top = self.header.bottom + 15;
}
- (void)activityDidLayout {
    
    NSLog(@"%lf,%lf,%lf", self.scroll.height, self.activityView.bottom + 15,self.activityView.bottom);
    self.scroll.contentSize = CGSizeMake(0, self.activityView.bottom + 15);
}
- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    
    _locService.delegate = nil;
    _locService = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    // 启动LocationService
    [_locService startUserLocationService];

    self.title = @"商家详情";
    
    self.view.backgroundColor = [UIColor orangeColor];
    
//    [self addBYItemviews:@[@"支付成功"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
    [self addNotify];

    [self addLeftBackBarButtonItem];
    
    [self createHeaderView];
    // 从商家列表进入
    if (self.shopsItemModel) {
        self.detailModel = [[ByModelShopsDetail alloc] init];
        self.detailModel.shopsItemModel = self.shopsItemModel;
//        [self.scroll addSubview:self.header];
        self.header.shopItemModel = self.detailModel.shopsItemModel;

    }

    [self getDetail];
    
    NSLog(@"%@",self.shopsItemModel);
}


#pragma mark - 定位处理
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    NSLog(@"正在定位：%@", userLocation.location);
    self.header.location = userLocation.location;
}

#pragma mark - 请求数据
- (void)getDetail {
    
    ByNetParamsModel *params = [ByNetParamsModel new];
    
    params.seller_id = self.type ? : [NSString stringWithFormat:@"%@", self.shopsItemModel.id];
    
    [self performSelector:@selector(load) withObject:nil afterDelay:0.01];
    
    [ByApiShops getShopsDetail:params success:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert hideHud];
        
        
        if (self.shopsItemModel) {
            self.detailModel = result.data;
            self.detailModel.shopsItemModel = self.shopsItemModel;
        } else {
            self.detailModel = result.data;
//            [self.scroll addSubview:self.header];
        }
        self.header.shopItemModel = self.detailModel.shopsItemModel;
        [self.scroll addSubview:self.activityView];
        self.activityView.activitys = self.detailModel.promotions;
        
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
}
- (void)load {
    
    [ByCoreToolAlert showLoadind:@"正在加载..."];
}

#pragma mark - 懒加载
- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];

        [self.view addSubview:_scroll];
    }
    return _scroll;
}

//- (ByViewShopsDetailHeader *)header {
//
//    if (!_header) {
//        _header = [[ByViewShopsDetailHeader alloc] initWithFrame:CGRectMake(10, 0, self.scroll.width - 20, 0)];
//        __weak typeof(self) weakself = self;
//        _header.buyBlock = ^ {
//
//            [weakself payOrder];
//        };
//        _header.shopItemModel = self.detailModel.shopsItemModel;
//    }
//    return _header;
//}
- (ByViewShopsDeatilActivity *)activityView {
    
    if (!_activityView) {
        _activityView = [[ByViewShopsDeatilActivity alloc] initWithFrame:CGRectMake(10, 0, self.scroll.width - 20, 0)];
    }
    return _activityView;
}

#pragma mark - 创建视图
- (void)createHeaderView{
    
    self.header = [[ByViewShopsDetailHeader alloc] initWithFrame:CGRectMake(10, 0, self.scroll.width - 20, 0)];
    __weak typeof(self) weakself = self;
    self.header.buyBlock = ^ {
        
        [weakself payOrder];
    };
    [self.scroll addSubview:self.header];
}

#pragma mark - 点击事件
-(void)leftItemsAction:(NSInteger)leftIndex{

    if (self.type) {
        [self by_normalPopVC:@"ByUIScanCodeVC"];
    }
    else{
        [super leftItemsAction:leftIndex];
    }
}

- (void)rightItemsAction:(NSInteger)rightIndex {
    
    ByUIShopsPaySuccessVC *vc = [[ByUIShopsPaySuccessVC alloc] init];
    [self by_normalPush:vc hasNavi:YES];
}
- (void)payOrder {
    
    ByUIPayVC *vc = [[ByUIPayVC alloc] init];
    vc.detailModel = self.detailModel;
    [self by_normalPush:vc hasNavi:YES];
}



@end
